<template>
  <teleport to="body">
    <div 
      v-if="visible"
      :class="[
        'bx-drawer',
        'bx-glass',
        `bx-drawer--${placement}`,
        {
          'bx-drawer--open': visible
        }
      ]"
      :style="drawerStyle"
    >
      <!-- 遮罩层 -->
      <div 
        v-if="mask"
        class="bx-drawer__mask"
        @click="handleMaskClick"
      ></div>

      <!-- 抽屉内容 -->
      <div 
        :class="[
          'bx-drawer__content',
          'bx-glass'
        ]"
        :style="contentStyle"
      >
        <!-- 头部 -->
        <div 
          v-if="title || $slots.title || closable"
          class="bx-drawer__header"
        >
          <div class="bx-drawer__title">
            <slot name="title">{{ title }}</slot>
          </div>
          
          <button
            v-if="closable"
            class="bx-drawer__close"
            @click="handleClose"
          >
            <BxIcon name="x" />
          </button>
        </div>

        <!-- 主体内容 -->
        <div class="bx-drawer__body">
          <slot></slot>
        </div>

        <!-- 底部 -->
        <div 
          v-if="$slots.footer"
          class="bx-drawer__footer"
        >
          <slot name="footer"></slot>
        </div>
      </div>
    </div>
  </teleport>
</template>

<script>
import { computed, watch, onMounted, onUnmounted } from 'vue'
import BxIcon from '../basic/BxIcon.vue'

export default {
  name: 'BxDrawer',
  components: {
    BxIcon
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ''
    },
    placement: {
      type: String,
      default: 'right',
      validator: (value) => ['top', 'right', 'bottom', 'left'].includes(value)
    },
    width: {
      type: [String, Number],
      default: 378
    },
    height: {
      type: [String, Number],
      default: 378
    },
    closable: {
      type: Boolean,
      default: true
    },
    mask: {
      type: Boolean,
      default: true
    },
    maskClosable: {
      type: Boolean,
      default: true
    },
    destroyOnClose: {
      type: Boolean,
      default: false
    },
    zIndex: {
      type: Number,
      default: 1000
    }
  },
  emits: ['update:visible', 'close', 'open'],
  setup(props, { emit }) {
    const drawerStyle = computed(() => {
      return {
        zIndex: props.zIndex
      }
    })

    const contentStyle = computed(() => {
      const style = {}
      
      if (['left', 'right'].includes(props.placement)) {
        const width = typeof props.width === 'number' ? `${props.width}px` : props.width
        style.width = width
        style.maxWidth = '100vw'
      } else {
        const height = typeof props.height === 'number' ? `${props.height}px` : props.height
        style.height = height
        style.maxHeight = '100vh'
      }
      
      return style
    })

    const handleClose = () => {
      emit('update:visible', false)
      emit('close')
    }

    const handleMaskClick = () => {
      if (props.maskClosable) {
        handleClose()
      }
    }

    const handleEscapeKey = (event) => {
      if (event.key === 'Escape' && props.visible) {
        handleClose()
      }
    }

    watch(() => props.visible, (newVal) => {
      if (newVal) {
        emit('open')
        // 禁止页面滚动
        document.body.style.overflow = 'hidden'
      } else {
        // 恢复页面滚动
        document.body.style.overflow = ''
      }
    })

    onMounted(() => {
      document.addEventListener('keydown', handleEscapeKey)
    })

    onUnmounted(() => {
      document.removeEventListener('keydown', handleEscapeKey)
      // 确保恢复页面滚动
      document.body.style.overflow = ''
    })

    return {
      drawerStyle,
      contentStyle,
      handleClose,
      handleMaskClick
    }
  }
}
</script>

<style scoped>
.bx-drawer {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  pointer-events: none;
  z-index: 1000;
}

.bx-drawer--open {
  pointer-events: auto;
}

.bx-drawer__mask {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(4px);
  animation: bx-drawer-mask-fade-in 0.3s ease-out;
}

.bx-drawer__content {
  position: absolute;
  background: var(--bx-glass-bg);
  backdrop-filter: var(--bx-glass-blur-strong);
  border: 1px solid var(--bx-glass-border);
  display: flex;
  flex-direction: column;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.2);
}

/* 不同位置的抽屉 */
.bx-drawer--right .bx-drawer__content {
  top: 0;
  right: 0;
  bottom: 0;
  border-radius: var(--bx-radius-large) 0 0 var(--bx-radius-large);
  animation: bx-drawer-slide-in-right 0.3s ease-out;
}

.bx-drawer--left .bx-drawer__content {
  top: 0;
  left: 0;
  bottom: 0;
  border-radius: 0 var(--bx-radius-large) var(--bx-radius-large) 0;
  animation: bx-drawer-slide-in-left 0.3s ease-out;
}

.bx-drawer--top .bx-drawer__content {
  top: 0;
  left: 0;
  right: 0;
  border-radius: 0 0 var(--bx-radius-large) var(--bx-radius-large);
  animation: bx-drawer-slide-in-top 0.3s ease-out;
}

.bx-drawer--bottom .bx-drawer__content {
  bottom: 0;
  left: 0;
  right: 0;
  border-radius: var(--bx-radius-large) var(--bx-radius-large) 0 0;
  animation: bx-drawer-slide-in-bottom 0.3s ease-out;
}

.bx-drawer__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 24px;
  border-bottom: 1px solid var(--bx-glass-border);
  flex-shrink: 0;
}

.bx-drawer__title {
  font-size: 16px;
  font-weight: 600;
  color: var(--bx-text-primary);
  margin: 0;
}

.bx-drawer__close {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  color: var(--bx-text-secondary);
  cursor: pointer;
  border-radius: var(--bx-radius-small);
  transition: all var(--bx-transition-medium);
}

.bx-drawer__close:hover {
  background: var(--bx-glass-bg-hover);
  color: var(--bx-text-primary);
}

.bx-drawer__body {
  flex: 1;
  padding: 24px;
  overflow-y: auto;
  font-family: var(--bx-font-family);
  color: var(--bx-text-primary);
}

.bx-drawer__footer {
  padding: 16px 24px;
  border-top: 1px solid var(--bx-glass-border);
  flex-shrink: 0;
  background: rgba(255, 255, 255, 0.02);
}

/* 动画效果 */
@keyframes bx-drawer-mask-fade-in {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}

@keyframes bx-drawer-slide-in-right {
  0% {
    transform: translateX(100%);
  }
  100% {
    transform: translateX(0);
  }
}

@keyframes bx-drawer-slide-in-left {
  0% {
    transform: translateX(-100%);
  }
  100% {
    transform: translateX(0);
  }
}

@keyframes bx-drawer-slide-in-top {
  0% {
    transform: translateY(-100%);
  }
  100% {
    transform: translateY(0);
  }
}

@keyframes bx-drawer-slide-in-bottom {
  0% {
    transform: translateY(100%);
  }
  100% {
    transform: translateY(0);
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .bx-drawer--right .bx-drawer__content,
  .bx-drawer--left .bx-drawer__content {
    width: 85vw !important;
    max-width: 85vw !important;
  }
  
  .bx-drawer--top .bx-drawer__content,
  .bx-drawer--bottom .bx-drawer__content {
    height: 85vh !important;
    max-height: 85vh !important;
  }
  
  .bx-drawer__header {
    padding: 16px 20px;
  }
  
  .bx-drawer__title {
    font-size: 15px;
  }
  
  .bx-drawer__body {
    padding: 20px;
  }
  
  .bx-drawer__footer {
    padding: 12px 20px;
  }
  
  .bx-drawer__close {
    width: 28px;
    height: 28px;
  }
}
</style>

